﻿using System;
using System.Collections.Generic;
//
using PerceptiveMCAPI.Types;
//
namespace PerceptiveMCAPI_Test
{
    public partial class get_ecommOrder : System.Web.UI.UserControl
    {
        public ApiMethod method { get; set; }
        public ecommAddOrder_order ecommAddOrder { get { return GetEcommAddOrder(); } }
        public campaignEcommAddOrder_order campaignEcommAddOrder { get { return GetCampaignEcommAddOrder(); } }

        // -----------------------
        int it; double dbl;
        // 
        private string order_id
        {
            get { return tbOrder_id.Text.Trim(); }
            set { tbOrder_id.Text = value; }
        }
        private DateTime order_date
        {
            get
            {
                DateTime dt;
                DateTime.TryParse( tbOrder_date.Text.Trim(), out dt );
                return dt;
            }
            set { tbOrder_date.Text = value.Date.ToShortDateString(); }
        }
        private string campaignId
        {
            get { return tbCampaignId.Text.Trim(); }
            set { tbCampaignId.Text = value; }
        }
        private string email_id
        {
            get { return tbEmail_id.Text.Trim(); }
            set { tbEmail_id.Text = value; }
        }
        private string email_address
        {
            get { return tbEmail_Address.Text.Trim(); }
            set { tbEmail_Address.Text = value; }
        }
        private double total
        {
            get
            {
                double.TryParse( tbTotal.Text.Trim(), out dbl );
                return dbl;
            }
            set { tbTotal.Text = value.ToString(); }
        }
        private double shipping
        {
            get
            {
                double.TryParse( tbShipping.Text.Trim(), out dbl );
                return dbl;
            }
            set { tbShipping.Text = value.ToString(); }
        }
        private double tax
        {
            get
            {
                double.TryParse( tbTax.Text.Trim(), out dbl );
                return dbl;
            }
            set { tbTax.Text = value.ToString(); }
        }
        private string store_id
        {
            get { return tbStore_id.Text.Trim(); }
            set { tbStore_id.Text = value; }
        }
        private string store_name
        {
            get { return tbStore_name.Text.Trim(); }
            set { tbStore_name.Text = value; }
        }
        private string plugin_id
        {
            get { return tbPlugin_id.Text.Trim(); }
            set { tbPlugin_id.Text = value; }
        }
        // --------------------------------------------
        public void Setup ()
        {
            this.Visible = true;
            lblSection.Text = method.section;
            lblMethod.Text = method.name;
            //
            if ( this.method.name == "campaignEcommAddOrder" )
                this.div_emailadr.Visible = false;
            else
                this.div_emailadr.Visible = true;
            //
            tbOrder_id.Focus();
            order_date = DateTime.Now;
        }
        // ------------------------------------------------------------------------------
        private ecommAddOrder_order GetEcommAddOrder ()
        {
            ecommAddOrder_order order = new ecommAddOrder_order();
            order = GetOrder( order ) as ecommAddOrder_order;
            return order;
        }
        private campaignEcommAddOrder_order GetCampaignEcommAddOrder ()
        {
            campaignEcommAddOrder_order order = new campaignEcommAddOrder_order();
            order = GetOrder( order ) as campaignEcommAddOrder_order;
            return order;
        }
        // -------------------------------------------------------------------------------------
        //
        public ecommOrder_order GetOrder<T> ( T order )
        {
            ecommOrder_order result = order as ecommOrder_order;
            result.order_id = this.order_id;
            result.order_date = this.order_date;
            result.campaign_id = this.campaignId;
            result.plugin_id = this.plugin_id;
            result.email_id = this.email_id;
            result.email = this.email_address;
            result.total = this.total;
            result.shipping = this.shipping;
            result.tax = this.tax;
            result.store_id = this.store_id;
            result.store_name = this.store_name;
            result.items = GetOrderItems();
            //
            return result;
        }
        private List<ecommOrder_item> GetOrderItems ()
        {
            List<ecommOrder_item> itemList = new List<ecommOrder_item>();
            ecommOrder_item item;
            //
            if ( !string.IsNullOrEmpty( prod_id_01.Text.Trim() ) )
            {
                item = new ecommOrder_item();
                int.TryParse( line_num_01.Text.Trim(), out it );
                item.line_num = it;
                int.TryParse( prod_id_01.Text.Trim(), out it );
                item.product_id = it;
                item.product_name = prod_name_01.Text.Trim();
                int.TryParse( cat_id_01.Text.Trim(), out it );
                item.category_id = it;
                item.category_name = cat_name_01.Text.Trim();
                int.TryParse( qty_01.Text.Trim(), out it );
                item.qty = it;
                double.TryParse( cost_01.Text.Trim(), out dbl );
                item.cost = dbl;
                //
                itemList.Add( item );
            }
            //
            if ( !string.IsNullOrEmpty( prod_id_02.Text.Trim() ) )
            {
                item = new ecommOrder_item();
                int.TryParse( line_num_02.Text.Trim(), out it );
                item.line_num = it;
                int.TryParse( prod_id_02.Text.Trim(), out it );
                item.product_id = it;
                item.product_name = prod_name_02.Text.Trim();
                int.TryParse( cat_id_02.Text.Trim(), out it );
                item.category_id = it;
                item.category_name = cat_name_02.Text.Trim();
                int.TryParse( qty_02.Text.Trim(), out it );
                item.qty = it;
                double.TryParse( cost_02.Text.Trim(), out dbl );
                item.cost = dbl;
                //
                itemList.Add( item );
            }
            //
            if ( !string.IsNullOrEmpty( prod_id_03.Text.Trim() ) )
            {
                item = new ecommOrder_item();
                int.TryParse( line_num_03.Text.Trim(), out it );
                item.line_num = it;
                int.TryParse( prod_id_03.Text.Trim(), out it );
                item.product_id = it;
                item.product_name = prod_name_03.Text.Trim();
                int.TryParse( cat_id_03.Text.Trim(), out it );
                item.category_id = it;
                item.category_name = cat_name_03.Text.Trim();
                int.TryParse( qty_03.Text.Trim(), out it );
                item.qty = it;
                double.TryParse( cost_03.Text.Trim(), out dbl );
                item.cost = dbl;
                //
                itemList.Add( item );
            }
            //
            if ( !string.IsNullOrEmpty( prod_id_04.Text.Trim() ) )
            {
                item = new ecommOrder_item();
                int.TryParse( line_num_04.Text.Trim(), out it );
                item.line_num = it;
                int.TryParse( prod_id_04.Text.Trim(), out it );
                item.product_id = it;
                item.product_name = prod_name_04.Text.Trim();
                int.TryParse( cat_id_04.Text.Trim(), out it );
                item.category_id = it;
                item.category_name = cat_name_04.Text.Trim();
                int.TryParse( qty_04.Text.Trim(), out it );
                item.qty = it;
                double.TryParse( cost_04.Text.Trim(), out dbl );
                item.cost = dbl;
                //
                itemList.Add( item );
            }
            //
            if ( !string.IsNullOrEmpty( prod_id_05.Text.Trim() ) )
            {
                item = new ecommOrder_item();
                int.TryParse( line_num_05.Text.Trim(), out it );
                item.line_num = it;
                int.TryParse( prod_id_05.Text.Trim(), out it );
                item.product_id = it;
                item.product_name = prod_name_05.Text.Trim();
                int.TryParse( cat_id_05.Text.Trim(), out it );
                item.category_id = it;
                item.category_name = cat_name_05.Text.Trim();
                int.TryParse( qty_05.Text.Trim(), out it );
                item.qty = it;
                double.TryParse( cost_05.Text.Trim(), out dbl );
                item.cost = dbl;
                //
                itemList.Add( item );
            }
            //
            return itemList;
        }
    }
}